#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: demo.py 
@version:
@time: 2022/04/24 
"""
import time

from TapClientConnector import TapClientConnector

# 数据库信息及服务信息
from TapUtil.TLinux.Tlinux import UseLinux

from db_info import Mysql

# 探活及服务启动
T = UseLinux()
cmd_start = ''
cmd_stop = ''
cmd_ps = ''
docker_id = Mysql.docker_mysql_5562.get('serve').get('docker')
if docker_id:
    cmd_start = f'docker start {docker_id}'
    cmd_stop = f'docker stop {docker_id}'
    cmd_ps = f'docker ps -a|grep {docker_id}'
try:
    TapClientConnector(Mysql.docker_mysql_5562)
except Exception as e:
    print(e)
    print('数据库连接失败')
    T.ssh_cmd(cmd_start)
    while True:
        try:
            TapClientConnector(Mysql.docker_mysql_5562)
            break
        except Exception as e:
            time.sleep(10)
print('服务启动成功')
t = TapClientConnector(Mysql.docker_mysql_5562)
print(t.tapclient_connector.show_dbs())
# 服务停止
T.ssh_cmd(cmd_stop)
# 查看容器状态
res = T.ssh_cmd(cmd_ps)
print(res)
T.close()
